package pharao.client.views.game;

import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Panel;

import pharao.client.dto.UserDTO;
import pharao.client.views.game.login.LoginController;
import pharao.client.views.game.login.LoginView;
import pharao.client.views.game.login.events.SetLoginUserEvent;
import pharao.client.views.game.login.events.ShowLoginViewEvent;
import pharao.client.views.game.navigation.GameNavigationController;
import pharao.client.views.game.navigation.GameNavigationModel;
import pharao.client.views.game.navigation.GameNavigationView;
import pharao.client.views.game.registrierung.RegistrierungsController;
import pharao.client.views.game.registrierung.RegistrierungsView;
import pharao.client.views.game.registrierung.events.ShowRegistrierungsViewEvent;
import pharao.client.views.navigation.events.AddGameNavigationEvent;
import hmvc4gwt.client.component.ComponentSingleChild;
import hmvc4gwt.client.component.IComponent;
import hmvc4gwt.client.event.IHmvcEvent;
import hmvc4gwt.framework.client.controller.GuiController;
import hmvc4gwt.framework.client.events.AddViewEvent;
import hmvc4gwt.framework.client.events.CloseViewEvent;
import hmvc4gwt.framework.client.events.TerminateEvent;

public class GameController extends GuiController {

	/**
	 * enthält die GameNavigation und kapselt somit das Event-Handling vom Spiel selbst und dem Rest der Anwendung
	 */
	private IComponent gameNavigation;
	
	/**
	 * wurde das Spiel gestartet
	 */
	private boolean gameStarted = false;
	
	/**
	 * enthält den einzuloggenden User
	 */
	private UserDTO loginUser;
	
	
	protected void initImpl() {
		
		
		
		/*
		 * 
		 */
		register(ShowRegistrierungsViewEvent.class, this);
		register(ShowLoginViewEvent.class, this);
		register(AddGameNavigationEvent.class, this);
		register(SetLoginUserEvent.class, this);
		
	}

	public void handleEventImpl(IHmvcEvent event) {
		
		if (event instanceof ShowRegistrierungsViewEvent) {
			
			/*
			 * registrieren anzeigen
			 */
			addChildComponent(new RegistrierungsController(),
					new RegistrierungsView());
			fireEvent(new ShowRegistrierungsViewEvent(), false, false, true);
			
		} else if (event instanceof ShowLoginViewEvent) {
			
			fireEvent(new CloseViewEvent(), false, true, true);

			/*
			 * login anzeigen
			 */
			addChildComponent(new LoginController(), new LoginView());
			fireEvent(new ShowLoginViewEvent(), false, false, true);
			
		} else if (event instanceof AddGameNavigationEvent) {
			
			if (gameStarted == true) {
				return;
			}
			
			gameStarted = true;
			
			/*
			 * GameContainer anzeigen
			 */
			fireEvent(new AddViewEvent(getView()), true, false, false);
			
			/*
			 * GameNavigation hinzufügen
			 * TODO: prüfen ob loginUser exsistiert
			 */
			gameNavigation = new ComponentSingleChild(new GameNavigationController(getLoginUser()), new GameNavigationModel(), new GameNavigationView((Panel) getView().getWidget()));
			
			/*
			 * GameMenu anzeigen
			 */
			History.newItem(GameNavigationModel.linkGameMenu);
			
		} if (event instanceof SetLoginUserEvent) {
			
			if (event.getPayload() instanceof UserDTO) {
				loginUser = (UserDTO) event.getPayload();
			}
			
		}

	}

	public Class getClassOfObject() {
		return GameController.class;
	}
	
	public void terminate() {
		gameStarted = false;
		gameNavigation.fireEvent(new TerminateEvent(), false, true, true);
	}

	private UserDTO getLoginUser() {
		return loginUser;
	}

	private void setLoginUser(UserDTO loginUser) {
		this.loginUser = loginUser;
	}

}
